package com.chagee.store.operation.server.utils;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.ttl.TransmittableThreadLocal;
import com.chagee.personnel.api.event.auth.UserAuthInfo;
import com.chagee.store.operation.server.service.biz.UserBizService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author yangyang
 * @date 2024/2/24-13:58
 **/
@Slf4j
@Component
public class UserUtil  {
	private static final ThreadLocal<UserAuthInfo> USER_THREAD_LOCAL =new TransmittableThreadLocal<>();
	public static UserAuthInfo getUser(){
		if (Objects.isNull(USER_THREAD_LOCAL.get())){
			synchronized (UserUtil.class){
				if (Objects.isNull(USER_THREAD_LOCAL.get())){
					USER_THREAD_LOCAL.set(new UserAuthInfo());
				}
			}
		}
		return USER_THREAD_LOCAL.get();
	}
	public static void setUser(UserAuthInfo userAuthInfo){
		USER_THREAD_LOCAL.set(userAuthInfo);
	}

	public static void remove(){
		USER_THREAD_LOCAL.remove();
	}

	public static UserBizService getBean(){
		return SpringUtil.getBean(UserBizService.class);
	}
}
